package com.binyuan.move.thried;

import com.binyuan.move.config.RecorderConfig;
import com.binyuan.move.service.IScreenRecorderMoveService;
import com.binyuan.move.utils.FileUtils;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SpringContextUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import static com.binyuan.move.thried.MouseMovementThread.SKIMMING;

@Component
public class DeleteFileThread {

    ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);

    private Logger logger = LoggerFactory.getLogger(DeleteFileThread.class);
    private RecorderConfig recorderConfig = null;
    IScreenRecorderMoveService moveService = null;
    public void start(){
        //获取bean
        recorderConfig = SpringContextUtil.getBean(RecorderConfig.class);
        moveService   = SpringContextUtil.getBean(IScreenRecorderMoveService.class);

        Runnable task = () -> {
            logger.info("执行删除文件操作");
            //删除文件
            File file = new File(recorderConfig.getSaveTo() + SKIMMING + DateUtils.getYester7day());
            if (file.exists() && file.isDirectory()){
                FileUtils.deleteFolder(file);
                moveService.deleteScreenRecorderMoveByMoveCreaTime(DateUtils.getYester7day());
            }
            file = null;
            logger.info("删除文件成功执行完成");
        };

        executor.scheduleAtFixedRate(task,0, 1, TimeUnit.DAYS);

    }


}
